package Reminder.domain;

import java.io.*;

public class Utils {
    public static int getFileSize(String path) {
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            return (int) file.length();
        }
        else{
            return 0;
        }
    }
    public static Message loadTXT(String path) throws IOException {
        String res = new String("");
        FileInputStream fis = null;
        if(!new File(path).exists()){
            return new Message("loadTxT","null","error");
        }
        else{
            try {
                fis = new FileInputStream(path);
                InputStreamReader reader = new InputStreamReader(fis,"GBK"); //最后的"GBK"根据文件属性而定，如果不行，改成"UTF-8"试试
                BufferedReader br = new BufferedReader(reader);
                String line;
                while ((line = br.readLine()) != null) {
                    res += line;
                }
                br.close();
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return new Message("loadTxT",res,"success");
        }
    }
    public static Message setTXT(String path,String content){
        FileOutputStream fos = null;
        try {

            fos = new FileOutputStream(path);
            fos.write(content.getBytes());
            fos.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Message("setTxT","修改失败","error");
        }
        finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new Message("setTxT","修改成功","success");
    }
}
